<?php
class PhotoController extends Zend_Controller_Action
{
    const PF_ICON_PATH = "css/icon/performance/%s.png";
    
    const PHOTO_FULL_PATH = "%s/full/%s/%s.jpg";
    const FULL_PATH_UNKNOWN = "%s/full/unknown.png";
    const THUMB_PATH = "%s/thumb/%s/%s.jpg";
    const THUMB_PATH_UNKNOWN = "%s/thumb/unknown.%s.png";
        
    const USER_THUMB_WIDTH_MAX = 28;
    const CLIENT_THUMB_WIDTH_MAX = 45;
    const GROUP_THUMB_WIDTH_MAX = 320;
    
    private $_path;
    private $_cat;
    private $_id;
    private $_pf;
    private $_width;
    
    private static $_cats = array('i','u','g');
    private static $_perf = array('red','orange','yellow','green');
    
    public function init() 
    {
        if (in_array($this->_request->getParam('cat'), self::$_cats)) {
            $this->_cat = $this->_request->getParam('cat');
            $this->_id = (int) $this->_request->getParam('id');
            $this->_setPathAndWidth();
        } else {
            exit; //throw exception
        }
        if (in_array($this->_request->getParam('pf'), self::$_perf)) {
            $this->_pf = $this->_request->getParam('pf');
        }
    }
    
    public function fullAction()
    {
        $this->view->photo = new HTech_Image($this->_path);
        if($this->_pf) {
            $this->view->photo->setStamp(sprintf(self::PF_ICON_PATH,$this->_pf));
        }
    } 
    
    public function thumbAction()
    {
        if(!file_exists($this->_path)) {
            $fullPath = sprintf(self::PHOTO_FULL_PATH, Zend_Registry::get('HT_Config')->photo->path, $this->_cat, $this->_id);
            if(file_exists($fullPath)) {
                $thumb = new HTech_Image_Thumbnail($fullPath);
                $thumb->setPath($this->_path)->setWidth($this->_width)->setHeight(3000)->setQuality(100)->create();
            } else {
                $this->_path = sprintf(self::THUMB_PATH_UNKNOWN, Zend_Registry::get('HT_Config')->photo->path, $this->_cat);
            }
        }        
        $this->view->photo = new HTech_Image($this->_path);
        if($this->_pf) {
            $this->view->photo->setStamp(sprintf(self::PF_ICON_PATH,$this->_pf))->setStampSx(-2)->setStampSy(-2);
        }
    }
    
    private function _setPathAndWidth()
    {
        switch($this->_request->getActionName())
        {
            case 'thumb':
                $this->_path = sprintf(self::THUMB_PATH, Zend_Registry::get('HT_Config')->photo->path, $this->_cat, $this->_id);
                switch($this->_cat) 
                { 
                    case 'u': $this->_width = (int) $this->_request->getParam('w') ? 
                            min(self::USER_THUMB_WIDTH_MAX, (int) $this->_request->getParam('w')) : self::USER_THUMB_WIDTH_MAX; break;
                    case 'i': $this->_width = (int) $this->_request->getParam('w') ? 
                            min(self::CLIENT_THUMB_WIDTH_MAX, (int) $this->_request->getParam('w')) : self::CLIENT_THUMB_WIDTH_MAX; break;
                    case 'g': $this->_width = (int) $this->_request->getParam('w') ? 
                            min(self::GROUP_THUMB_WIDTH_MAX, (int) $this->_request->getParam('w')) : self::GROUP_THUMB_WIDTH_MAX; break;
                }
                break;
            case 'full':
                $this->_path = file_exists(sprintf(self::PHOTO_FULL_PATH, Zend_Registry::get('HT_Config')->photo->path, $this->_cat, $this->_id)) ? 
                    sprintf(self::PHOTO_FULL_PATH, Zend_Registry::get('HT_Config')->photo->path, $this->_cat, $this->_id) :
                    sprintf(self::FULL_PATH_UNKNOWN, Zend_Registry::get('HT_Config')->photo->path);
                break;
            default:
                exit; //throw new Exception;
        }
        return $this;
    }
}
